package com.example.demo.controller;
import com.example.demo.entity.*;
import com.example.demo.service.BorrowService;
import com.example.demo.service.ClassroomService;
import com.example.demo.service.PublicareaService;
import com.example.demo.util.RunTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * (Borrow)表控制层
 *
 * @author makejava
 * @since 2021-10-12 15:32:50
 */
@Controller
public class BorrowController {
    /**
     * 服务对象
     */
    @Autowired
    private BorrowService borrowService;

    @Autowired
    private ClassroomService classroomService;

    @Autowired
    private PublicareaService publicareaService;

    //通过ID查询单条数据
    @RequestMapping("selectOneBorrow")
    @ResponseBody
    public BorrowEntity selectOneBorrow(Integer id) {
        return this.borrowService.queryBorrowById(id);
    }

    //借教室
    @RequestMapping("classroomBorrow")
    @ResponseBody
    public int classroomBorrow(int cid, int bid, String bortime, HttpServletRequest request){

        HttpSession session = request.getSession();
        UserEntity userEntity = (UserEntity)session.getAttribute("userEntity");

        List<BorrowEntity> borrowEntities = borrowService.queryBorrowByUidList(userEntity.getUid());

        int j = 0;
        for (BorrowEntity b :
                borrowEntities) {
            if (b.getBorflag() == 0){
                j = 1;
            }
        }

        if (j == 0){

            BuildingEntity buildingEntity = new BuildingEntity();
            buildingEntity.setBid(bid);
            ClassroomEntity classroomEntity = classroomService.queryClassroomById(cid);
            //1、获取当前时间，获取到的时间类型是long类型的，单位是毫秒
            long currentTime = System.currentTimeMillis() ;
            //2、在这个基础上加上
            currentTime += Integer.parseInt(bortime) * 60 * 60 * 1000;
            //3、格式化时间，获取到的就是当前时间半个小时之后的时间
            Date date=new Date(currentTime);
            //4、建立时间格式化对象：
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //结束时间
            String format = dateFormat.format(date);
            //当前时间
            String nowData = dateFormat.format(new Date());

            BorrowEntity borrowEntity = new BorrowEntity();
            borrowEntity.setUid(userEntity);
            borrowEntity.setBid(buildingEntity);
            borrowEntity.setBorname(classroomEntity.getCname());
            borrowEntity.setBortime(nowData + "~" +format + "  共计" + bortime + "小时");
            borrowEntity.setBorflag(0);

            int i = borrowService.insertBorrow(borrowEntity);

            if (i == 1){
                classroomEntity.setCflag(2);
                classroomService.updateClassroom(classroomEntity);
            }
            return i;
        }else {
            return 0;
        }

    }

    //借公共区域
    @RequestMapping("publicareaBorrow")
    @ResponseBody
    public int publicareaBorrow(int paid, int bid, String bortime, HttpServletRequest request){

        HttpSession session = request.getSession();
        UserEntity userEntity = (UserEntity)session.getAttribute("userEntity");

        List<BorrowEntity> borrowEntities = borrowService.queryBorrowByUidList(userEntity.getUid());

        int j = 0;
        for (BorrowEntity b : borrowEntities) {
            if (b.getBorflag() == 0){
                j = 1;
            }
        }

        if (j == 0){

            BuildingEntity buildingEntity = new BuildingEntity();
            buildingEntity.setBid(bid);

            PublicAreaEntity publicAreaEntity = publicareaService.queryPublicareaById(paid);

            //1、获取当前时间，获取到的时间类型是long类型的，单位是毫秒
            long currentTime = System.currentTimeMillis() ;
            //2、在这个基础上加上
            currentTime += Integer.parseInt(bortime) * 60 * 60 * 1000;
            //3、格式化时间，获取到的就是当前时间半个小时之后的时间
            Date date=new Date(currentTime);
            //4、建立时间格式化对象：
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //结束时间
            String format = dateFormat.format(date);
            //当前时间
            String nowData = dateFormat.format(new Date());

            BorrowEntity borrowEntity = new BorrowEntity();
            borrowEntity.setUid(userEntity);
            borrowEntity.setBid(buildingEntity);
            borrowEntity.setBorname(publicAreaEntity.getPaname());
            borrowEntity.setBortime(nowData + "~" +format + "  共计" + bortime + "小时");
            borrowEntity.setBorflag(0);

            int i = borrowService.insertBorrow(borrowEntity);

            if (i == 1){
                publicAreaEntity.setPaflag(1);
                publicareaService.updatePublicarea(publicAreaEntity);
            }
            return i;
        }else {
            return 0;
        }

    }

    //计时
    @RequestMapping("checkTimeBorrow")
    public String checkTimeBorrow(HttpServletRequest request){
        HttpSession session = request.getSession();
        UserEntity userEntity = (UserEntity)session.getAttribute("userEntity");
        int uid = userEntity.getUid();
        BorrowEntity borrowEntity = borrowService.queryBorrowByUid(uid);
        String bortime = borrowEntity.getBortime();

        String midStrBefore ="共计";
        String before = bortime.substring(0,bortime.indexOf(midStrBefore)+midStrBefore.length());

        String midStrAll ="小时";
        String all = bortime.substring(0,bortime.indexOf(midStrAll)+midStrAll.length());

        String hour = bortime.substring(before.length(),all.length()-2);

        RunTimeUtil runTimeUtil = new RunTimeUtil();
//        int i = runTimeUtil.RunTime(Integer.parseInt(hour)*60*60);
        int i = runTimeUtil.RunTime(Integer.parseInt(hour));

        if (i == 0){
            String borname = borrowEntity.getBorname();
            if (borname.length() == 4){
                ClassroomEntity classroomEntities = classroomService.queryClassroomByBorname(borname);
                if (classroomEntities.getCflag() == 2){
                    classroomEntities.setCflag(0);
                    classroomService.updateClassroom(classroomEntities);
                    borrowEntity.setBorflag(1);
                    borrowService.updateBorrow(borrowEntity);
                }
            }else if (borname.length() == 2){
                PublicAreaEntity publicAreaEntity = publicareaService.queryPublicareaByPaname(borname);
                if(publicAreaEntity.getPaflag() == 1){
                    publicAreaEntity.setPaflag(0);
                    publicareaService.updatePublicarea(publicAreaEntity);
                    borrowEntity.setBorflag(1);
                    borrowService.updateBorrow(borrowEntity);
                }
            }
        }
        return "forward:toClassroomBorrow";
    }

    //查看所有借
    @RequestMapping("toShowMyBorrow")
    public String queryBorrowByUidList(HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserEntity userEntity = (UserEntity)session.getAttribute("userEntity");
        List<BorrowEntity> borrowEntities = borrowService.queryBorrowByUidList(userEntity.getUid());
        request.setAttribute("borrowEntities",borrowEntities);
        return "showMyBorrow";
    }

}